package cn.arikeky.org.controller;

import cn.arikeky.basic.config.BussinessException;
import cn.arikeky.basic.util.AjaxResult;
import cn.arikeky.basic.util.PageList;
import cn.arikeky.org.domain.DictionaryDetail;
import cn.arikeky.org.query.DictionaryDetailQuery;
import cn.arikeky.org.service.IDictionaryDetailService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class DictionaryDetailController {

    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    /**
     * 添加/修改
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody DictionaryDetail dictionaryDetail){
        if (null == dictionaryDetail.getId()){
            // 添加
            dictionaryDetailService.add(dictionaryDetail);
        }else {
            // 修改
            dictionaryDetailService.update(dictionaryDetail);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            dictionaryDetailService.delete(id);
        } catch(BussinessException ex){
            ex.printStackTrace();
            return AjaxResult.me().fail("参数不能为空");
        }

        return AjaxResult.me().success();
    }

    /**
     * 查询所有
     */
    @GetMapping
    @ApiOperation(value = "查询所有部门数据" )
    public List<DictionaryDetail> findAll(){
        return dictionaryDetailService.findAll();
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("/{id}")
    public DictionaryDetail findById(@PathVariable("id") Long id){
        return dictionaryDetailService.findById(id);
    }

    /**
     *
     * 分页查询
     */
    @PostMapping
    public PageList<DictionaryDetail> queryByPage(@RequestBody DictionaryDetailQuery query){
        return dictionaryDetailService.queryByPage(query);
    }
}
